import 'package:flutter/material.dart';
import 'package:zrjz/comm/utils/ex_widget.dart';

// ignore: slash_for_doc_comments
/**
    TextView + 容器的使用
    内部使用各种容器包裹Text控件，用于一些复杂的UI效果

    模板：
    TextView(
    "自定义textview自定义textview自定义textview自定义textview自定义textview",
    backgroundColor: Colors.red,
    textColor: Colors.white,
    padding: 10,
    cornerRadius: 10,
    borderColor: Colors.yellow,
    borderWidth: 1,
    marginTop: 5,
    singleLine: false,
    onClick: (){
    print("点击事件");
    }),
 */
class MyTextView extends StatelessWidget {
  double? fontSize = 0;
  Color? textColor = Colors.black;
  String content = "";
  bool? isTextEllipsis = false;
  bool? isFontLight;
  bool? isFontRegular;
  bool? isFontMedium;
  bool? isFontBold;
  FontWeight? fontWeight;
  TextAlign? textAlign;
  int? maxLines;

  //底部是Text的父容器属性
  VoidCallback? onClick;
  double? cornerRadius = 0;
  double? borderWidth = 0;
  Color? borderColor = Colors.transparent;
  double? boxWidth; //限制TextView的宽高，为外部 Container 的属性
  double? boxHeight; //限制TextView的宽高，为外部 Container 的属性
  AlignmentGeometry? alignment;
  Color? backgroundColor = Colors.transparent;
  double? padding = 0;
  double? margin = 0;
  double? paddingLeft = 0;
  double? paddingRight = 0;
  double? paddingTop = 0;
  double? paddingBottom = 0;
  double? marginLeft = 0;
  double? marginRight = 0;
  double? marginTop = 0;
  double? marginBottom = 0;
  final ClickType type; //默认没有点击类型
  final int milliseconds; //点击类型的时间戳（毫秒）

  MyTextView(
    this.content, {
    Key? key,
    this.textColor,
    this.backgroundColor,
    this.padding,
    this.paddingTop,
    this.paddingBottom,
    this.paddingRight,
    this.paddingLeft,
    this.cornerRadius,
    this.borderColor,
    this.borderWidth,
    this.marginBottom,
    this.marginLeft,
    this.marginRight,
    this.marginTop,
    this.margin,
    this.maxLines,
    this.fontSize,
    this.isTextEllipsis,
    this.isFontLight,
    this.isFontRegular,
    this.isFontMedium,
    this.isFontBold,
    this.fontWeight,
    this.textAlign,
    this.boxWidth,
    this.boxHeight,
    this.onClick,
    this.type = ClickType.none, //默认没有点击类型
    this.milliseconds = 500, //点击类型的时间戳（毫秒）
  }) : super(key: key) {
    if (padding != null && padding! > 0) {
      paddingLeft = padding;
      paddingRight = padding;
      paddingBottom = padding;
      paddingTop = padding;
    }

    if (margin != null && margin! > 0) {
      marginLeft = margin;
      marginTop = margin;
      marginRight = margin;
      marginBottom = margin;
    }

    if (isFontLight != null && isFontLight!) {
      fontWeight = FontWeight.w300;
    } else if (isFontRegular != null && isFontRegular!) {
      fontWeight = FontWeight.w400;
    } else if (isFontMedium != null && isFontMedium!) {
      fontWeight = FontWeight.w500;
    } else if (isFontBold != null && isFontBold!) {
      fontWeight = FontWeight.w700;
    } else {
      fontWeight = fontWeight ?? FontWeight.normal;
    }
  }

  @override
  Widget build(BuildContext context) {
    //如果需要 Container 就用 Container 包裹，否则直接返回Text
    if (onClick != null ||
        (cornerRadius ?? 0) > 0 ||
        (borderWidth ?? 0) > 0 ||
        boxWidth != null ||
        boxHeight != null ||
        backgroundColor != Colors.transparent ||
        (padding ?? 0) > 0 ||
        (margin ?? 0) > 0 ||
        (paddingLeft ?? 0) > 0 ||
        (paddingRight ?? 0) > 0 ||
        (paddingRight ?? 0) > 0 ||
        (paddingTop ?? 0) > 0 ||
        (paddingBottom ?? 0) > 0 ||
        (marginLeft ?? 0) > 0 ||
        (marginRight ?? 0) > 0 ||
        (marginTop ?? 0) > 0 ||
        (marginBottom ?? 0) > 0 ||
        alignment != null) {
      return _warpWithContainer();
    } else {
      return _childText();
    }
  }

  //TextView需要用容器包裹
  Widget _warpWithContainer() {
    return Container(
      width: boxWidth,
      height: boxHeight,
      alignment: alignment,
      margin: EdgeInsets.fromLTRB(marginLeft ?? 0, marginTop ?? 0, marginRight ?? 0, marginBottom ?? 0),
      decoration: BoxDecoration(
        border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
        color: backgroundColor,
        borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
      ),
      padding: EdgeInsets.fromLTRB(paddingLeft ?? 0, paddingTop ?? 0, paddingRight ?? 0, paddingBottom ?? 0),
      child: onClick != null
          ? _childText().onTap(  //使用扩展的方式点击
              onClick!,
              type: type,
              milliseconds: milliseconds,
            )
          : _childText(),
    );
  }

  //真正的Tex控件配置
  Widget _childText() {
    return Text(
      content,
      textAlign: textAlign ?? TextAlign.start,
      maxLines: maxLines,
      style: TextStyle(
        color: textColor,
        fontSize: fontSize ?? 14,
        fontWeight: fontWeight,
        overflow: isTextEllipsis ?? false ? TextOverflow.ellipsis : TextOverflow.clip,
      ),
    );
  }
}
